package VUTHANHPHUC.SERIZABLE;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;

public class Views extends JFrame {

	private JPanel contentPane;
	private JTextField txtMSV;
	private JTextField txtHoten;
	private JTextField txtDiem;
	private JTextField txtKQ;
	private List<Students> lisStudent;
	private Students objStudent;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Views frame = new Views();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Views() {
		setTitle("Thông tin sinh viên");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 269);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("Xử lí thông tin");
		lblNewLabel.setBounds(0, 0, 104, 14);
		contentPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("Mã Sinh Viên:");
		lblNewLabel_1.setBounds(10, 42, 94, 14);
		contentPane.add(lblNewLabel_1);

		JLabel lblHVTn = new JLabel("Họ và tên");
		lblHVTn.setBounds(10, 71, 94, 14);
		contentPane.add(lblHVTn);

		JLabel lblim = new JLabel("Điểm");
		lblim.setBounds(10, 104, 94, 14);
		contentPane.add(lblim);

		JLabel lblNewLabel_2 = new JLabel("Kêt quả");
		lblNewLabel_2.setBounds(10, 139, 94, 14);
		contentPane.add(lblNewLabel_2);

		txtMSV = new JTextField();
		txtMSV.setBounds(114, 39, 273, 20);
		contentPane.add(txtMSV);
		txtMSV.setColumns(10);

		txtHoten = new JTextField();
		txtHoten.setColumns(10);
		txtHoten.setBounds(114, 68, 273, 20);
		contentPane.add(txtHoten);

		txtDiem = new JTextField();
		txtDiem.setColumns(10);
		txtDiem.setBounds(114, 101, 273, 20);
		contentPane.add(txtDiem);

		txtKQ = new JTextField();
		txtKQ.setColumns(10);
		txtKQ.setBounds(114, 136, 273, 20);
		contentPane.add(txtKQ);

		JButton btnNewButton = new JButton("Ghi dữ thông tin");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Controllers<Students> objC = new Controllers<Students>();
				try {
		            JFileChooser objFile = new JFileChooser();
		            objFile.setCurrentDirectory(new java.io.File("."));
		            objFile.setDialogTitle("Chọn file: ");
		            objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		            String[] EXTENSION = new String[] { "dat" };
		            FileNameExtensionFilter filter = new FileNameExtensionFilter("DAT files", EXTENSION);
		            objFile.setFileFilter(filter);
		            objFile.setMultiSelectionEnabled(false);

		            int returnVal = objFile.showSaveDialog(null);
		            String ParentPath = "";
		            String FileName = "";
		            if (returnVal == JFileChooser.APPROVE_OPTION) {
		                ParentPath = objFile.getSelectedFile().getParent();
		                FileName = objFile.getSelectedFile().getName();
		            }

		            if (objC.WriteListObjectIntoFiles(lisStudent, ParentPath, FileName)) {
		                txtKQ.setText("Ghi file thành công");
		            }
		        } catch (Exception e) {
		            txtKQ.setText("Ghi file thất bại");
		            e.printStackTrace();
		        }
			}
		});
		btnNewButton.setBounds(114, 172, 127, 23);
		contentPane.add(btnNewButton);

		JButton btnThmThngTin = new JButton("Thêm thông tin");
		btnThmThngTin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (txtMSV.getText().isEmpty() || txtHoten.getText().isEmpty() || txtDiem.getText().isEmpty()) {
					JOptionPane.showMessageDialog(getParent(), "Yêu cầu nhập đầy đủ thông tin", "Thông báo",
							JOptionPane.ERROR_MESSAGE);
					;
				} else {
					lisStudent = new ArrayList<Students>();
					objStudent = new Students();
					objStudent.setRollNo(Integer.parseInt(txtMSV.getText()));
					objStudent.setFullName(txtHoten.getText());
					objStudent.setMark(Double.parseDouble(txtDiem.getText()));
					
					lisStudent.add(objStudent);
					txtKQ.setText("Có: "+lisStudent.size()+" sinh viên trong danh sách");
					
					txtMSV.setText("");
					txtHoten.setText("");
					txtDiem.setText("");
				}
			}
		});
		btnThmThngTin.setBounds(267, 172, 120, 23);
		contentPane.add(btnThmThngTin);
	}
}
